<?php
/**
 * Moduł odpowiedzialny za pokazywanie wyników
 * @author Miron Pawlik
 */

if($fd[0] == 'result') {
    list($scheduleId, $subjectId) = explode(':', $fd[1]);
    if(is_numeric($scheduleId)) {
        $smarty->assign('scheduleId', $scheduleId);
        $subjects = $db->getData('subjects', false, 'subjectID', 'name');
        $smarty->assign('subjects', $subjects);

        $smarty->assign('fullResult', true);
        $smarty->assign('terms', $allTerms);


        if(is_numeric($subjectId)) {
            $subject = $db->getById($subjectId, 'subjects', '*', 'subjectID');
            $smarty->assign('subject', $subject);
            $allTerms = $db->getAll('terms', 'day, week, timeStart', 'subjectID = '.$subjectId);
            $smarty->assign('terms', $allTerms);

            $tmp = $db->query("SELECT s.name as name, s.surname as surname, s.indexID as indexID, t.termID as termID FROM chosenTerms as ct JOIN students as s ON s.indexID = ct.indexID JOIN terms AS t ON t.genID = ct.termID WHERE ct.scheduleId = $scheduleId AND ct.subjectID = $subjectId ORDER BY s.surname, s.name");
            $result = $tmp->fetchAll();
            $tmp->closeCursor();

            $terms = array();
            foreach($result as $res) {
                $terms[$res['termID']][] = $res;
            }
            $smarty->assign('result', $terms);

        }
    }
    else {
        header('Location: '.$_conf['host']);
    }
}
//wypisywanie wyników dla pojedynczej osoby
else {
    if(is_numeric($fd[1])) {
        $q = $db->query("SELECT s.name as name, s.color as color, t.timeStart as start, t.timeEnd as end, t.day as day, t.week as week, t.teacher as teacher, t.room as room FROM chosenTerms as ct JOIN subjects as s ON ct.subjectID = s.subjectID JOIN terms as t ON ct.termID = t.genID AND ct.subjectID = t.subjectID WHERE ct.scheduleId = :scheduleId AND ct.indexID = :indexId ORDER BY day, start");
        $q->bindValue(':indexId', $user->getId());
        $q->bindValue(':scheduleId', $fd[1]);
        $res = $q->execute();
        $results = $q->fetchAll();
        $smarty->assign('results', $results);
        if($fd[0] == 'table') {
            $extendedTemplate = 'table';
            $tableResults = array();
            foreach($results as $res) {
                if($res['week'] == 0) {
                    $tableResults[$res['day']][$res['start']][1] = array($res['name'], $res['week'], $res['end']-$res['start'], $res['color'], $res['teacher'], $res['room']);
                    $tableResults[$res['day']][$res['start']][2] = 1;
                    for($i = $res['start']+1; $i< $res['end']; $i++) {
                        $tableResults[$res['day']][$i][1] = 1;
                        $tableResults[$res['day']][$i][2] = 1;
                    }
                }
                else {
                    $tableResults[$res['day']][$res['start']][$res['week']] = array($res['name'], $res['week'], $res['end']-$res['start'], $res['color'], $res['teacher'], $res['room']);
                    for($i = $res['start']+1; $i< $res['end']; $i++) {
                        $tableResults[$res['day']][$i][$res['week']] = 2;
                    }

                }
            }
            //dla wykładów
            $query = $db->query("SELECT * FROM subjects AS s JOIN studentSubjects AS ss ON ss.subjectID = s.subjectID WHERE ss.indexID = :indexId");
            $query->bindValue(':indexId', $user->getId());
            $query->execute();
            $subjects = $query->fetchAll();
            $subject = array();
            foreach($subjects as $res) {
                if($res['week'] == 0) {
                    $tableResults[$res['day']][$res['start']][1] = array($res['name'], $res['week'], $res['end']-$res['start'], $res['color'], $res['teacher'], $res['room']);
                    $tableResults[$res['day']][$res['start']][2] = 1;
                    for($i = $res['start']+1; $i< $res['end']; $i++) {
                        $tableResults[$res['day']][$i][1] = 1;
                        $tableResults[$res['day']][$i][2] = 1;
                    }
                }
                else {
                    $tableResults[$res['day']][$res['start']][$res['week']] = array($res['name'], $res['week'], $res['end']-$res['start'], $res['color'], $res['teacher'], $res['room']);
                    for($i = $res['start']+1; $i< $res['end']; $i++) {
                        $tableResults[$res['day']][$i][$res['week']] = 2;
                    }

                }
            }
            $smarty->assign('results', $tableResults);

        }
        else if($fd[0] == 'ical') {
            echo 'adf';
        }
        $smarty->assign('scheduleId', $fd[1]);
    }
    else {
        $schedules = $db->getAll('schedules');
        if(count($schedules) == 1) {
            header('Location: '.$_conf['host'].'?schedule,'.$schedules[0]['scheduleID']);
        }
        else {
            $smarty->assign('schedules', $schedules);
        }

    }
}
$smarty->assign('week', $weeks);
$smarty->assign('days', $days);
$smarty->assign('hours', $hours);
?>